We will, again, use data from Gapminder for the following exercises on relational data.
There is an R package named gapminder that contains a selection of the data from Gapminder.
Our aim for this exercises is to use individual data files from Gapminder to create combined datasets that resemble the one included in the gapminder package.
Before we can begin with these exercises, we need to load the datasets. To do this, you can simply use the following code (that repeats some parts of the previous exercises).
library(tidyverse)
gap_cont <- read_csv("../data/countries_continent.csv")
gap_life <- read_csv("../data/life_expectancy_years.csv")
gap_pop <- read_csv("../data/population_total.csv")
gap_gdp <- read_csv("../data/gdppercapita_us_inflation_adjusted.csv")
gap_fert <- read_csv("../data/children_per_woman_total_fertility.csv")
gap_cont <- gap_cont %>%
mutate(continent = as_factor(continent))
First of all, let’s have a look at the Gapminder data that is included in the gapminder package.
gapminder package and have a look at the dataset it contains. The dataset we want is simply called gapminder.
In the following, we will use different joins to create datasets that contain the same set of variables. We will create two versions of the combined dataset.
Before we do this, however, we want to explore the overlap and discrepancies between the individual datasets. This is somewhat easier to do with the datasets in wide format (as each country name only appears in one row in those).
anti_join() for this task. To just get the country names, you can select the country variable.
For the following series of joins, we want the data in the long format again. As we have not really discussed this topic (only briefly mentioned it), just copy, paste, and run the code below to transform the datasets accordingly.
gap_life <- gap_life %>%
pivot_longer(-country,
names_to = "year",
values_to = "life_exp") %>%
mutate(year = as.numeric(year))
gap_pop <- gap_pop %>%
pivot_longer(-country,
names_to = "year",
values_to = "pop") %>%
mutate(year = as.numeric(year))
gap_gdp <- gap_gdp %>%
pivot_longer(-country,
names_to = "year",
values_to = "gdp_percap") %>%
mutate(year = as.numeric(year))
gap_fert <- gap_fert %>%
pivot_longer(-country,
names_to = "year",
values_to = "fert") %>%
mutate(year = as.numeric(year))
As stated above, we want to create two different versions of the combined datasets: One without missing data and one with as many observations (rows) as possible.
Create the two versions of the combined dataset described above using one type of mutating join for each one.
For the one without missing data you should start with the dataset with the largest number of countries in it (gap_pop), and then join the other datasets in descending order of the number of countries they contain (gap_life, gap_gdp, gap_cont). This datasets also requires two additional (pipe) steps to ensure that a) it contains no NAs and b) its variables are in the same order as in the gapminder package dataset.
gap_nomiss and gap_full.
The types of mutating joins that you should use are inner_join() and full_join. The variables you should join on are country and year.
gap_nomiss dataset requires two additional steps.